<html>
<head><meta charset="utf-8"><title>Redundant copy elision · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Redundant.20copy.20elision.html">Redundant copy elision</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222512866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Redundant%20copy%20elision/near/222512866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chase Wilson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Redundant.20copy.20elision.html#222512866">(Jan 12 2021 at 23:53)</a>:</h4>
<p>I've been wanting to get into mir-opt for a while and I noticed that a lot of times rustc generates mir with what seems to have redundant copies, such as this code</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="kt">u8</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u8</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">y</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>which generates this mir in release mode</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">add</span><span class="p">(</span><span class="n">_1</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">_2</span>: <span class="kt">u8</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u8</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">debug</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">_1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">debug</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">_2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">_0</span>: <span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">_3</span>: <span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">_4</span>: <span class="kt">u8</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="n">bb0</span>: <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">StorageLive</span><span class="p">(</span><span class="n">_3</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">_3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_1</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">StorageLive</span><span class="p">(</span><span class="n">_4</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">_4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_2</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">_0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Add</span><span class="p">(</span><span class="k">move</span><span class="w"> </span><span class="n">_3</span><span class="p">,</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="n">_4</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">StorageDead</span><span class="p">(</span><span class="n">_4</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">StorageDead</span><span class="p">(</span><span class="n">_3</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>and this mir in debug mode</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">add</span><span class="p">(</span><span class="n">_1</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">_2</span>: <span class="kt">u8</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u8</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">debug</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">_1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">debug</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">_2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">_0</span>: <span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">_3</span>: <span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">_4</span>: <span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">_5</span>: <span class="p">(</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">bool</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="n">bb0</span>: <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">StorageLive</span><span class="p">(</span><span class="n">_3</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">_3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_1</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">StorageLive</span><span class="p">(</span><span class="n">_4</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">_4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_2</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">_5</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">CheckedAdd</span><span class="p">(</span><span class="n">_3</span><span class="p">,</span><span class="w"> </span><span class="n">_4</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">assert</span><span class="p">(</span><span class="o">!</span><span class="k">move</span><span class="w"> </span><span class="p">(</span><span class="n">_5</span><span class="p">.</span><span class="mi">1</span>: <span class="kt">bool</span><span class="p">),</span><span class="w"> </span><span class="s">"attempt to compute `{} + {}`, which would overflow"</span><span class="p">,</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="n">_3</span><span class="p">,</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="n">_4</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">bb1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="n">bb1</span>: <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">_0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="p">(</span><span class="n">_5</span><span class="p">.</span><span class="mi">0</span>: <span class="kt">u8</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">StorageDead</span><span class="p">(</span><span class="n">_4</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">StorageDead</span><span class="p">(</span><span class="n">_3</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=ae055d346e2f7135ea49af2c0713598e">Playground</a><br>
<a href="https://godbolt.org/z/xTrjv1">Godbolt (Release)</a><br>
<a href="https://godbolt.org/z/8bYPnj">Godbolt (Debug)</a></p>
<p>If eliding the redundant copies so that the mir looked something like this</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">add</span><span class="p">(</span><span class="n">_1</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">_2</span>: <span class="kt">u8</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u8</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">debug</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">_1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">debug</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">_2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">_0</span>: <span class="kt">u8</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="n">bb0</span>: <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">_0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Add</span><span class="p">(</span><span class="k">move</span><span class="w"> </span><span class="n">_1</span><span class="p">,</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="n">_2</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>is valid, I'd love to work on it if anyone can point me in the right direction.</p>
<p>In looking at the generated llvm-ir it does seem like llvm's automatic ssa simplification gets rid of the extra variables in a simple case like this, but that could change in more complex code. I do think it should be worth it regardless, any simplification of the ir helps with other optimizations and things like miri</p>



<a name="222514468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Redundant%20copy%20elision/near/222514468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Redundant.20copy.20elision.html#222514468">(Jan 13 2021 at 00:11)</a>:</h4>
<p>The previous work in this area might be of interest: <a href="https://github.com/rust-lang/rust/issues/72632">#72632</a>, <a href="https://github.com/rust-lang/rust/issues/76723">#76723</a>.</p>



<a name="222517928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Redundant%20copy%20elision/near/222517928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chase Wilson <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Redundant.20copy.20elision.html#222517928">(Jan 13 2021 at 00:56)</a>:</h4>
<p>The latter has by far the best pr name &amp; branch title I've ever seen, thank you for the links!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>